<?php

/**
 * 自动生成 WxEventShakeAwardService
 * 
 * Apps/Common/Service/WxEventShakeAwardService.class.php
 * Zix @ 2015-09-03
 */

namespace Common\Service;

class WxEventShakeAwardService {

  private static $instance;
  var $rank = array(
    1 => '一等奖',
    2 => '二等奖',
    3 => '三等奖',
    4 => '四等奖',
    5 => '五等奖',
    6 => '六等奖',
    7 => '七等奖',
    8 => '八等奖',
    9 => '九等奖',
  );
  
  var $type = array(
    1  => '兑换码'   ,
    2  => '实物'     ,
    3  => '微信红包' ,
    99 => '未中奖' 
  );
  
  var $status = array(
    0 => '禁用',
    1 => '启用',
  );
  
  public static function instance() {
    if (self::$instance == null) {
      $instance = new WxEventShakeAwardService();
    }

    return $instance;
  }

  function get_default_row() {
    return array(
      'rank' => '1',
      'type' => '1',
      'name' => '',
      'icon' => '',
      'amount' => '0.00',
      'password' => '',
      'sn_url' => '',
      'take_times' => 0 ,
      'total_qty' => '0',
      'taken_qty' => '0',
      'remain_qty' => '0',
      'take_rate' => 0 ,
      'status' => '1',
    );
  }

  function get_by_id($id) {
    $WxEventShakeAward = M('WxEventShakeAward');
    $data = $WxEventShakeAward->find($id);
    return $data ? $data : array();
  }

  function get_by_cond($config) {
    $default = array(
      'have_remain' => FALSE ,
      'page' => 1,
      'page_size' => 20 ,
      'status' => '',
      'event_id' => '' ,
      'have_remain' => FALSE ,
      'count' => FALSE,
      'order' => 'ASC',
      'sort' => 'rank',
    );

    $config = extend($config, $default);

    $WxEventShakeAward = M('WxEventShakeAward');

    $where = array();

    if (!empty($config['status'])) {
      $where['status'] = $config['status'];
    }
    
    if (!empty($config['event_id'])) {
      $where['event_id'] = $config['event_id'];
    }
    
    if ( $config['have_remain'] ) {
      $where['remain_qty'] = array('gt' , 0 );
    }

    if ($config['count']) {
      return $WxEventShakeAward->where($where)->count();
    } else {
      $order = $config['sort'] . ' ' . $config['order'];
      $limit = ($config['page'] - 1 ) * $config['page_size'] . ' , ' . $config['page_size'];
      $data = $WxEventShakeAward
          ->where($where)
          ->limit($limit)->order($order)->select();
      //echo $WxEventShakeAward->_sql();
      return $data ? $data : array();
    }
  }
  
  function get_by_rank( $event_id , $rank ) {
    $WxEventShakeAward = M('WxEventShakeAward');
    $where = array(
      'event_id' => $event_id ,
      'rank' => $rank ,
    );
    
    $data = $WxEventShakeAward->where( $where )->find();
    return $data ? $data : array();
  }

  function create( $data ) {
    $old_data = $this->get_by_rank( $data['event_id'], $data['rank']);
    if ( !empty( $old_data ) ) {
      return ajax_arr( $this->rank[$data['rank']] . '已经有了') ;
    }
    
    $data['remain_qty'] = $data['total_qty'] ;
    $WxEventShakeAward = M('WxEventShakeAward');
    $ret = $WxEventShakeAward->add($data);

    if ($ret) {
      return ajax_arr('添加成功', TRUE, array(
        'id' => $ret
      ));
    } else {
      return ajax_arr('添加失败', FALSE);
    }
  }

  function update($id, $data) {
    $old_data = $this->get_by_rank( $data['event_id'], $data['rank']);
    if ( !empty( $old_data ) && $old_data['id'] != $id ) {
      return ajax_arr( $this->rank[$data['rank']] . '已经有了' ) ;
    }
    
    $WxEventShakeAward = M('WxEventShakeAward');

    $ret = $WxEventShakeAward->where("id = %d", $id)->save($data);
    if ($ret) {
      return ajax_arr('编辑成功', TRUE);
    } else {
      return ajax_arr('编辑失败', FALSE);
    }
  }

  function delete($ids) {
    $WxEventShakeAward = M('WxEventShakeAward');
    $ret = $WxEventShakeAward->delete($ids);
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }

}
